<?php

namespace common\models\datas;

use common\components\behaviors\UpdateCacheBehavior;
use common\components\helpers\EventData;
use Yii;
use yii\helpers\ArrayHelper;
use common\components\helpers\Util;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%integral_config}}".
 *
 * @property integer $id
 * @property string $name
 * @property integer $event_id
 * @property string $variable
 * @property string $type_id
 */
class IntegralConfig extends \common\components\bases\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%integral_config}}';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [

            [['event_id'], 'required'],
            [['event_id'], 'integer'],
            [['variable'], 'string'],
            [['name'], 'string', 'max' => 100],
            //[['id','name','event_id','variable'],'string']
        ];
    }


    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('common', '自动编号'),
            'name' => Yii::t('common', '积分名称'),
            'event_id' => Yii::t('common', '事件编号'),
            'variable' => Yii::t('common', '变量'),
        ];
    }

    public function fields()
    {
        $field = parent::fields();
        $field['event_id'] = function () {
            return (new EventData())->getNameByKey($this->event_id);
        };

        $field['variable'] = function () {
            $v = Util::array2text(unserialize($this->variable));
            $v = nl2br($v);

            return $v;
        };
        return $field;
    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'integralconfig'
            ],
        ]);
    }
}
